import constDef from '@/js/ConstDef'
import funcService from '@/api/FuncService'
import cacheManage from '@/cache/CacheManage'
// eslint-disable-next-line no-unused-vars
const frontRuleServices = {
  funcService, cacheManage
}
class FrontRuleExecutor {
  getRuleTexts(rules, ruleStage) {
    return rules.filter((item) => { return item.ruleStage === ruleStage })
  }

  executeFdLayoutRules(currentInstance, fvData, variable, ruleStage) {
    const rules = fvData.fdLayout.fdLayoutRules;
    const executeRules = this.getRuleTexts(rules, ruleStage);
    const ruleTexts = executeRules.map((item) => { return item.ruleText; });
    return this.evalFdLayoutScript(currentInstance, ruleTexts, fvData, variable);
  }

  executeLayoutRules(currentInstance, nvsData, variable, ruleStage) {
    const rules = nvsData.layout.layoutRules;
    const executeRules = this.getRuleTexts(rules, ruleStage);
    const ruleTexts = executeRules.map((item) => { return item.ruleText; });
    return this.evalLayoutScript(currentInstance, ruleTexts, nvsData, variable);
  }

  async evalLayoutScript(currentInstance, ruleTexts, nvsData, variable) {
    // eslint-disable-next-line no-unused-vars
    const that = currentInstance;
    // eslint-disable-next-line no-unused-vars
    const CodeDefConst = constDef;
    if (Array.isArray(ruleTexts)) {
      let result = true;
      for (const ruleText of ruleTexts) {
        // eslint-disable-next-line no-eval
        if ((await eval(ruleText)) !== true) {
          result = false;
          break;
        }
      }
      return result;
    } else {
      // eslint-disable-next-line no-eval
      return eval(ruleTexts)
    }
  }

  async evalFdLayoutScript(currentInstance, ruleTexts, fvData, variable) {
    // eslint-disable-next-line no-unused-vars
    const that = currentInstance;
    // eslint-disable-next-line no-unused-vars
    const CodeDefConst = constDef;
    if (Array.isArray(ruleTexts)) {
      let result = true;
      for (const ruleText of ruleTexts) {
        // eslint-disable-next-line no-eval
        if ((await eval(ruleText)) !== true) {
          result = false;
          break;
        }
      }
      return result;
    } else {
      // eslint-disable-next-line no-eval
      return eval(ruleTexts)
    }
  }
}
const frontRuleExecutor = new FrontRuleExecutor();
export default frontRuleExecutor;
